package com.mlink.vs.service;

import com.mlink.vs.entity.PiccUser;
import com.mlink.vs.entity.PiccUserOnlineStatus;
import com.mlink.vs.entity.UCResult;
import com.mlink.vs.sso.BaseAction;
import com.mlink.vs.util.Constants;

import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Generated;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * {@link PiccUserOnlineStatus}のサービスクラスです。
 * 
 */
@Generated(value = { "S2JDBC-Gen 2.4.47",
		"org.seasar.extension.jdbc.gen.internal.model.ServiceModelFactoryImpl" }, date = "2017-12-6 17:40:33")
public class PiccUserOnlineStatusService extends AbstractService<PiccUserOnlineStatus> {
	/**
	 * 識別子でエンティティを検索します。
	 * 
	 * @param userId
	 *            識別子
	 * @return エンティティ
	 */
	public PiccUserOnlineStatus findById(String userId) {
		return select().id(userId).getSingleResult();
	}

	public boolean isExist(String userId) {

		String sql = "select * from PICC_USER_ONLINE_STATUS  where USER_ID = ? ";
		List<PiccUserOnlineStatus> users = jdbcManager.selectBySql(PiccUserOnlineStatus.class, sql, userId)
				.getResultList();
		if (users.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

	public boolean isOnline(String userId) {
		String sql = "select * from PICC_USER_ONLINE_STATUS  where USER_ID = ? and IS_ONLINE=? ";
		List<PiccUserOnlineStatus> users = jdbcManager.selectBySql(PiccUserOnlineStatus.class, sql, userId, 1)
				.getResultList();
		if (users.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

	public List<PiccUserOnlineStatus> getOnlines() {
		String sql = "select * from PICC_USER_ONLINE_STATUS  where  IS_ONLINE = ? ";
		List<PiccUserOnlineStatus> users = jdbcManager.selectBySql(PiccUserOnlineStatus.class, sql, 1).getResultList();
		return users;
	}

	public int findIsOnlineCount(String isOnline, String userType1, String userType2,String groupId) {
		List<Object> params = new ArrayList<Object>();
		String sql = "select count(1) from PICC_USER_ONLINE_STATUS where IS_ONLINE=? and GROUP_ID=? and ( USER_TYPE=? or USER_TYPE=? )";

		params.add(isOnline);
		params.add(groupId);
		params.add(userType1);
		params.add(userType2);
		return jdbcManager.selectBySql(int.class, sql, params.toArray()).getSingleResult();
	}
	public List<PiccUserOnlineStatus> findIsOnline(String isOnline) {
		List<Object> params = new ArrayList<Object>();
		String sql = "select * from PICC_USER_ONLINE_STATUS where IS_ONLINE=? ";

		params.add(isOnline);
	
		return jdbcManager.selectBySql(PiccUserOnlineStatus.class, sql, params.toArray()).getResultList();
	}
	public List<PiccUserOnlineStatus> findIsOnline(String isOnline, String userType1, String userType2) {
		List<Object> params = new ArrayList<Object>();
		String sql = "select * from PICC_USER_ONLINE_STATUS where IS_ONLINE=?  and ( USER_TYPE=? or USER_TYPE=? )";

		params.add(isOnline);
	
		params.add(userType1);
		params.add(userType2);
		return jdbcManager.selectBySql(PiccUserOnlineStatus.class, sql, params.toArray()).getResultList();
	}
	public List<PiccUserOnlineStatus> findIsOnlineOfGroup(String isOnline, String userType1, String userType2, String groupId) {
		List<Object> params = new ArrayList<Object>();
		String sql = "select * from PICC_USER_ONLINE_STATUS where IS_ONLINE=?  and GROUP_ID=?  and( USER_TYPE=? or USER_TYPE=? )";

		params.add(isOnline);
		params.add(groupId);
		params.add(userType1);
		params.add(userType2);
		return jdbcManager.selectBySql(PiccUserOnlineStatus.class, sql, params.toArray()).getResultList();
	}

	public List<PiccUserOnlineStatus> findIsOnlinePerson(String isOnline, String groupID) {
		List<Object> params = new ArrayList<Object>();
		String sql = "select * from PICC_USER_ONLINE_STATUS where IS_ONLINE=? AND  GROUP_ID=?";

		params.add(isOnline);
		params.add(groupID);
		return jdbcManager.selectBySql(PiccUserOnlineStatus.class, sql, params.toArray()).getResultList();
	}
	public List<PiccUserOnlineStatus> findIsOnlinePerson(String isOnline) {
		List<Object> params = new ArrayList<Object>();
		String sql = "select * from PICC_USER_ONLINE_STATUS where IS_ONLINE=?";

		params.add(isOnline);
		return jdbcManager.selectBySql(PiccUserOnlineStatus.class, sql, params.toArray()).getResultList();
	}
	public int updateStatus(PiccUserOnlineStatus piccUserOnlineStatus) {
		String sql = "update PICC_USER_ONLINE_STATUS set IS_ONLINE = ? where USER_ID = ? ";
		return jdbcManager.updateBySql(sql, String.class, String.class)
				.params(piccUserOnlineStatus.isOnline, piccUserOnlineStatus.userId).execute();
	}

	public int updateStatusu(PiccUserOnlineStatus piccUserOnlineStatus) {
		String sql = "update PICC_USER_ONLINE_STATUS set IS_ONLINE = ?, USER_TYPE=?, GROUP_ID=? where USER_ID = ? ";
		return jdbcManager.updateBySql(sql, String.class, String.class,String.class, String.class)
				.params(piccUserOnlineStatus.isOnline, piccUserOnlineStatus.userType, piccUserOnlineStatus.groupId,piccUserOnlineStatus.userId)
				.execute();
	}
//
	// 获取用户所在组在线人数
	public int findIsOnlineCount(String userId, String token) {

		String isOnline = Constants.USER_STATUS_ONLINE;
		String userType1 = Constants.USER_TYPE_SEAT;
		String userType2 = Constants.USER_TYPE_LEADER;
		try {
			JSONObject body = new JSONObject();
			UCResult resuelt = null;

			body.put("TOKEN", token);
			resuelt = BaseAction.requeryUC("UAC_USER_GETMEMBERSOFGROUP", body);
			if (resuelt.code.equals("1")) {
				JSONArray users = resuelt.body.getJSONArray("users");
				List<PiccUserOnlineStatus> PiccUserList = findIsOnlinePerson(Constants.USER_STATUS_ONLINE);
				int count = 0;
				for (int i = 0; i < users.length(); i++) {
					JSONObject cell = users.getJSONObject(i);
					String userId1 = cell.getString("userId");
					for (int j = 0; j < PiccUserList.size(); j++) {
						PiccUserOnlineStatus  ps = 	PiccUserList.get(j);
						if(ps.userId.equals(userId1)) {
							count++;
							break;
						}
					}

				}
				return count;
			} else {
				return 0;

			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

}